/*
 * Niiuzu
 * Copyright (C) 2011  Thierry Nowak
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.niiuzu.client.screen.authentification.lost.password;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.LazyPresenter;
import com.niiuzu.client.eventbus.AuthentificationEventBus;
import com.niiuzu.client.i18n.AuthentificationMessages;
import com.niiuzu.client.util.ClientContextFactory;
import com.niiuzu.client.util.LoadingTicket;
import com.niiuzu.client.util.callback.AbstractCallback;
import com.niiuzu.shared.rpc.UserServiceAsync;

/**
 * Presenter du mot de passe perdu.
 */
@Presenter(view = LostPasswordViewImpl.class)
public class LostPasswordPresenter extends
		LazyPresenter<LostPasswordView, AuthentificationEventBus> {

	@Inject
	private UserServiceAsync userService;

	@Inject
	private AuthentificationMessages authentificationMessages;

	private ClickHandler closeClickHandler;
	private ClickHandler validateClickHandler;

	@Override
	public void bindView() {
		super.bindView();

		closeClickHandler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				view.hidePopup();
			}
		};
		view.getCloseButtonHasClickHandlers()
				.addClickHandler(closeClickHandler);
		view.getCancelHasClickHandlers().addClickHandler(closeClickHandler);

		validateClickHandler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				sendLostPasswordQuery();
			}
		};
		view.getValidationHasClickHandlers().addClickHandler(
				validateClickHandler);
	}

	/**
	 * L'utilisateur a indiqué qu'il a perdu son mot de passe.
	 * 
	 * @see AuthentificationEventBus#userPasswordHasBeenLost()
	 */
	public void onUserPasswordHasBeenLost() {

		view.getEmailAddressField().setValue("");
		view.getEmailAddressConfirmationField().setValue("");

		view.showPopup();

		view.focusEmailAddressField();
	}

	/**
	 * Envoie une requête au serveur suite à la perte d'un mot de passe.
	 */
	private void sendLostPasswordQuery() {
		String emailValue = view.getEmailAddressField().getValue();
		String emailConfValue = view.getEmailAddressConfirmationField()
				.getValue();

		if (emailValue.isEmpty()) {
			eventBus.businessErrorOccurred(authentificationMessages
					.errorMissingMailValue());
		} else if (emailConfValue.isEmpty()) {
			eventBus.businessErrorOccurred(authentificationMessages
					.errorMissingMailConfirmationValue());
		} else if (!emailValue.equals(emailConfValue)) {
			eventBus.businessErrorOccurred(authentificationMessages
					.errorInvalidMailAddresses());
		} else {

			String loadingMessage = authentificationMessages
					.lostPasswordWaitingMessage();
			final LoadingTicket ticket = LoadingTicket.Factory.createTicket(
					this.getClass(), loadingMessage);
			eventBus.loadingStarted(ticket);

			userService.solveLostPasswordProblem(emailValue,
					ClientContextFactory.get(), new AbstractCallback<Void>(
							eventBus, ticket) {
						@Override
						public void onSuccess(Void result) {
							onLostPasswordProblemSolved(ticket);
						}
					});

		}
	}

	/**
	 * Poursuit le traitement suite à la résolution du problème de mot de passe.
	 */
	void onLostPasswordProblemSolved(LoadingTicket ticket) {
		view.hidePopup();

		eventBus.loadingFinished(ticket);

		view.showConfirmationMessage(authentificationMessages
				.lostPasswordSolvedMessage());
	}

	/**
	 * Comportement d'un clic sur le bouton de fermeture de l'écran.
	 */
	ClickHandler getCloseClickHandler() {
		return closeClickHandler;
	}

	/**
	 * Comportement d'un clic sur le bouton de validation de l'écran.
	 */
	ClickHandler getValidateClickHandler() {
		return validateClickHandler;
	}

	/* ********************************************************************** */
	/* ********************************************************************** */
	/* ********************************************************************** */

	/**
	 * JUnit - Définit un mock pour {@link UserServiceAsync}.
	 */
	void setMockedUserService(UserServiceAsync mockedUserService) {
		this.userService = mockedUserService;
	}

	/**
	 * JUnit - Définit un mock pour {@link AuthentificationMessages}.
	 */
	public void setMockedAuthentificationMessages(
			AuthentificationMessages mockedAuthentificationMessages) {
		this.authentificationMessages = mockedAuthentificationMessages;
	}
}
